/*
 * Copyright (C) 2017-2021
 * All rights reserved, Designed By 深圳中科鑫智科技有限公司
 * Copyright authorization contact 18814114118
 */
package com.shop.cereshop.agent.service.agentAuth.impl;
import cn.hutool.core.util.DesensitizedUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.cereshop.agent.page.user.CereAgentUserVO;
import com.shop.cereshop.commons.constant.CoReturnFormat;
import com.shop.cereshop.commons.domain.agent.CereAgentRealnameAuth;
import com.shop.cereshop.commons.domain.user.faceCheck.FaceCheckModelTwo;
import com.shop.cereshop.commons.domain.user.faceCheck.respond.FaceCheckRespondTwo;
import com.shop.cereshop.commons.domain.userAuth.UserRealnameAuthRespond;
import com.shop.cereshop.agent.dao.agentAuth.AgentRealnameAuthDAO;
import com.shop.cereshop.agent.service.agentAuth.AgentRealnameAuthService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.time.LocalDateTime;
/**
 * <p>
 * 业务实现类
 * 用户实名认证表
 * </p>
 *
 * @author
 * @date 2025-08-07
 */
@Slf4j
@Service
public class AgentRealnameAuthServiceImpl extends ServiceImpl<AgentRealnameAuthDAO, CereAgentRealnameAuth>  implements AgentRealnameAuthService {

    @Resource
    private AgentRealnameAuthDAO userRealnameAuthDAO;

    @Override
    @Transactional
    public Integer saveUserAuth(CereAgentUserVO user, FaceCheckRespondTwo faceCheckRespond, FaceCheckModelTwo model, String phone)  {
        if (user==null){
           throw new RuntimeException(CoReturnFormat.USER_NOT_EXIST);
        }
        {
            CereAgentRealnameAuth userRealnameAuth = new CereAgentRealnameAuth();
            userRealnameAuth.setAgentUserId(user.getAgentUserId());
            userRealnameAuth.setRealName(model.getUserName());
            userRealnameAuth.setIdCardType(1);
            userRealnameAuth.setIdCardNumber(model.getIdCardNumber());
            userRealnameAuth.setAuthStatus(faceCheckRespond.getResultObject().getBizCode().equals("1") ? 2 : 3);
            userRealnameAuth.setAuthTime(LocalDateTime.now());
            userRealnameAuth.setIdCardHandheld(model.getFaceUrl());
            userRealnameAuth.setStatusCode(faceCheckRespond.getCode().equals("200") ? "200" : "500");
            userRealnameAuth.setPhone(phone);

           return userRealnameAuthDAO.insert(userRealnameAuth);

        }

    }

    @Override
    public UserRealnameAuthRespond findAuthInfoByUserId(Long agentUserId) {
        CereAgentRealnameAuth cereAgentRealnameAuth = userRealnameAuthDAO.selectOne(new QueryWrapper<CereAgentRealnameAuth>().eq("agent_user_id", agentUserId));
        if (cereAgentRealnameAuth == null) {
            return null;
        }
        return UserRealnameAuthRespond.builder()
                .userId(cereAgentRealnameAuth.getAgentUserId())
                .realName(DesensitizedUtil.chineseName(cereAgentRealnameAuth.getRealName()))
                .idCardNumber(DesensitizedUtil.idCardNum(cereAgentRealnameAuth.getIdCardNumber(), 6, 4))
                .idCardType(cereAgentRealnameAuth.getIdCardType())
                .idCardHandheld(cereAgentRealnameAuth.getIdCardHandheld())
                .build();
    }

    @Override
    public CereAgentRealnameAuth selectByIdCartNumber(String idCardNumber) {
        return  userRealnameAuthDAO.selectByIdCardNumber(idCardNumber);
    }
//
//    @Override
//    public Integer updateStatue(Integer statue, Long userId) {
//        return userRealnameAuthDAO.updateStatue(statue,userId);
//    }
}
